//
//  PassengerDetailViewController.m
//  JiongTu
//
//  Created by 杨璟 on 16/3/22.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "PassengerDetailViewController.h"

#import "JTPassengerTrip.h"
#import "JTUser.h"

#import "JTPushTool.h"

#import <AVOSCloud.h>
#import <SVProgressHUD.h>
#import "UIImageView+WebCache.h"

@interface PassengerDetailViewController ()

@property (nonatomic, strong) JTPassengerTrip *oneTrip;

@property (weak, nonatomic) IBOutlet UIImageView *bgIcon;

@property (weak, nonatomic) IBOutlet UIImageView *userIcon;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@property (weak, nonatomic) IBOutlet UILabel *startLabel;

@property (weak, nonatomic) IBOutlet UILabel *destinationLabel;

@property (weak, nonatomic) IBOutlet UILabel *priceLabel;

@property (weak, nonatomic) IBOutlet UILabel *seatNumberLabel;

@property (weak, nonatomic) IBOutlet UILabel *remarkLabel;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (weak, nonatomic) IBOutlet UIButton *button;

@end

@implementation PassengerDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"viewdidload");
    
    if (self.pushTrip) {
        self.oneTrip = self.pushTrip;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    NSLog(@"viewwillappear");
}

- (void)viewWillLayoutSubviews {
    self.userIcon.layer.cornerRadius = self.userIcon.bounds.size.width * 0.5;
    self.userIcon.layer.masksToBounds = YES;
    self.userIcon.layer.borderWidth = 1;
    self.userIcon.layer.borderColor = [UIColor lightGrayColor].CGColor;
}

- (void)setPushInfo:(NSDictionary *)pushInfo {
    _pushInfo = pushInfo;
    
    if (pushInfo[@"objectId"]) {
        [SVProgressHUD showWithStatus:@"查询中"];
        //进入时查询对应订单
        AVQuery * query = [AVQuery queryWithClassName:@"PassengerTrip"];
        [query whereKey:@"objectId" equalTo:pushInfo[@"objectId"]];
        
        [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
            if (!error) {
                [SVProgressHUD dismiss];
                self.oneTrip = [JTPassengerTrip tripWithAVObject:object];
            } else {
                [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            }
        }];
    }
}

- (void)setOneTrip:(JTPassengerTrip *)oneTrip {
    _oneTrip = oneTrip;
    
    NSLog(@"%@", oneTrip.passenger.nickname);
    
    self.nameLabel.text = oneTrip.passenger.nickname;
    self.countLabel.text = [NSString stringWithFormat:@"出行次数：%@", oneTrip.passenger.current];
    self.startLabel.text = [oneTrip.startLocation stringByAppendingString:oneTrip.startStreet];
    self.destinationLabel.text = [oneTrip.endLocation stringByAppendingString:oneTrip.endStreet];
    self.priceLabel.text = [NSString stringWithFormat:@"%@元", oneTrip.price];
    self.seatNumberLabel.text = [oneTrip.orderSeatNumber stringValue];
    self.timeLabel.text = oneTrip.time;
    if ([oneTrip.remark isEqualToString:@""]) {
        self.remarkLabel.text = @"无";
    } else {
        self.remarkLabel.text = oneTrip.remark;
    }
    
    [self.userIcon sd_setImageWithURL:oneTrip.passenger.iconPath placeholderImage:[UIImage imageNamed:@"头像占位"]];
    [self.bgIcon sd_setImageWithURL:oneTrip.passenger.iconPath placeholderImage:[UIImage imageNamed:@"头像占位"]];
    
    if ([oneTrip.isOrdered isEqual:@(0)]) {
        self.button.enabled = YES;
    } else {
        self.button.enabled = NO;
    }
}

- (IBAction)dismissClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"tempDriverTripID"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }];
}

- (IBAction)buttonPressed:(UIButton *)sender {
  
    sender.enabled = NO;
    [JTPushTool pushToPassenger:self.oneTrip withBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [SVProgressHUD showSuccessWithStatus:@"已通知乘客"];
            
            AVObject *theDriverTrip = [AVObject objectWithoutDataWithClassName:@"DriverTrip" objectId:[[NSUserDefaults standardUserDefaults] objectForKey:@"tempDriverTripID"]];
            [theDriverTrip addUniqueObject:self.oneTrip.objectId forKey:@"Passenger"];
#warning 无法限制增加
            [theDriverTrip incrementKey:@"current" byAmount:self.oneTrip.orderSeatNumber];
            [theDriverTrip saveInBackground];
            
            AVObject *theTrip = [AVObject objectWithoutDataWithClassName:@"PassengerTrip" objectId:self.oneTrip.objectId];
            [theTrip setObject:[AVUser currentUser] forKey:@"DriverInformation"];
#warning 后台添加一个行程字段存储司机接单的那一个行程
            [theTrip setObject:theDriverTrip forKey:@"DriverTripInformation"];
            //通知乘客时乘客的订单状态更改为已预约
            [theTrip setObject:@(1) forKey:@"IsOrdered"];
            [theTrip saveInBackground];
        }
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismissClick:nil];
    });
    
//    AVQuery * query = [AVQuery queryWithClassName:@"Driver"];
//    [query whereKey:@"DriverId" equalTo:[AVUser currentUser].objectId];
//    [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
//        AVRelation *relation = [object relationForKey:@"DriverTripInfo"];
//        AVQuery *relationQuery = [relation query];
//        [relationQuery orderByDescending:@"createdAt"];
////        self.latestDriverTripID = [relationQuery getFirstObject].objectId;
//    }];
    
}

@end
